package com.xuecheng.content.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.model.dto.QueryCourseParamsDto;
import com.xuecheng.model.po.CourseBase;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 课程基本信息 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseBaseServiceImpl extends ServiceImpl<CourseBaseMapper, CourseBase> implements CourseBaseService {

    @Resource
    private CourseBaseMapper courseBaseMapper;

    @Override
    public PageResult<CourseBase> queryCourseBaseList(PageParams params, QueryCourseParamsDto queryCourseParamsDto) {
        String auditStatus = queryCourseParamsDto.getAuditStatus();
        String courseName = queryCourseParamsDto.getCourseName();
        String publishStatus = queryCourseParamsDto.getPublishStatus();
        if (params == null) {
            throw new RuntimeException("Page params are empty.");
        }
        LambdaQueryWrapper<CourseBase> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(auditStatus), CourseBase::getAuditStatus, auditStatus)
                .like(StringUtils.isNotBlank(courseName), CourseBase::getName, courseName)
                .eq(StringUtils.isNotBlank(publishStatus), CourseBase::getStatus, publishStatus);
        IPage<CourseBase> page = new Page<>(params.getPageNo(), params.getPageSize());
        baseMapper.selectPage(page, wrapper);
        return new PageResult<CourseBase>(page.getRecords(), page.getTotal(), params.getPageNo(), params.getPageSize());
    }
}
